package com.qf.sy2103.ssm.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sy2103.ssm.Dao.NumberDao;
import com.qf.sy2103.ssm.Service.NumberService;
import com.qf.sy2103.ssm.entity.Number;
import com.qf.sy2103.ssm.vo.Tablevo;
import com.qf.sy2103.ssm.vo.pagevo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NumberServiceImpl implements NumberService {
    @Autowired
    private NumberDao numberDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<Number> getAll(pagevo pagevo, Number number) {
        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        List<Number> selectall = numberDao.selectall(number);
        PageInfo<Number> numberPageInfo = new PageInfo<>(selectall);
        return new Tablevo<Number>(0,"ok",numberPageInfo.getTotal(),selectall);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUse(Number number) {
        int i = numberDao.addNumber(number);
        if (i==0){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUse(List<Integer> ids) {
        int i = numberDao.deleteNumber(ids);
        if (i==0){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateUse(Number number) {
        int i = numberDao.updateNumber(number);
        if (i==0){
            return false;
        }
        return true;
    }

    @Override
    public Tablevo<Number> getALL(pagevo pagevo) {
        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        List<Number> selectall = numberDao.getALL(pagevo);
        PageInfo<Number> numberPageInfo = new PageInfo<>(selectall);
        return new Tablevo<Number>(0,"ok",numberPageInfo.getTotal(),selectall);
    }
}
